import { reactive, getCurrentInstance, onMounted } from "vue"
import { getAuthorization, setAuthorization } from "@/utils/auth.js"

let { proxy } = getCurrentInstance()

import {
    IMUserInfo
} from "@/api/im/index.js"
import SDK from "@yxim/nim-web-sdk";
import { useAccountStore } from "@/store/index.js"
let store = useAccountStore()
const onMsg = async (msg) => {
    let makeFriendsList = store.$state.makeFriendsList
    let arr = makeFriendsList.map(o => o.UserInfo.IMId)
    msg.chatType = 1
    msg.isRead = 1
    if (!arr.includes(msg.to)) {
        let data = await proxy.$get(IMUserInfo + msg.to)
        store.addMakeFriends(data.data)
    }
    store.addChatMsg(msg)
}

let curUserInfo = JSON.parse(getAuthorization("userInfo"))
let IMId = curUserInfo.IMId
let IM_ToKen = curUserInfo.IM_ToKen
window.nim = SDK.NIM.getInstance({
    debug: false, // 是否开启日志，将其打印到console。集成开发阶段建议打开。
    appKey: "181791f825dd689e4daac44fb7870ce4",
    account: IMId, //用于登录IM
    token: IM_ToKen, //用于登录IM
    db: true, //若不要开启数据库请设置false。SDK 默认为true。
    // privateConf: {}, // 私有化部署方案所需的配置
    // onconnect: onConnect,
    //消息收发的监听事件
    onmsg: onMsg,
});

const sendFn = () => {
    // let store = useAccountStore()
    // store.login(5)
    window.nim.sendText({
        scene: "p2p",
        to: "251707",
        text: "优秀11111111111111111111",
        done: sendMsgDone,
    });
    getF()
};

function sendMsgDone(error, msg) {
}
